var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var log4js = require('log4js');
var _ = require('lodash');
var log = log4js.getLogger("app");
var security = require('./lib/security');
// var index = require('./routes/index');
var users = require('./routes/users');//会员操作
var qn = require('./routes/qn'); //七牛云操作
var admin = require('./routes/admin');//后台管理人员操作
var type = require('./routes/type');//分类操作
var picture = require('./routes/picture');//图片操作
var recommend = require('./routes/recommend');//推荐操作
var article = require('./routes/article');//文章
var console = require('./routes/console');//控制台
var project = require('./routes/project');//项目
var systemlogs = require('./routes/systemlogs');//日志
var brand = require('./routes/brand');//品牌
var experience = require('./routes/experience');//体验
var openwx = require('./routes/openWX');//微信
var basicinfo = require('./routes/basicinfo');//基本信息
var feedback = require('./routes/feedback');//反馈
var cors = require('cors');


var passport = require('passport');
var pass = require('./lib/pass');

var app = express();
app.locals.settings = require('./config/settings');
app.locals.dirname = __dirname;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// app.use(logger('dev'));
app.use(log4js.connectLogger(log4js.getLogger("http"), { level: 'auto' }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.use(passport.initialize());
app.use(passport.session());

app.use(function (req, res, next) {
  //不需要过滤的地方（例如：登录、注册、短信验证...）
  if (security.no_auth.indexOf(req.path) > -1){
    //在这个地方不过滤url
    next();
  }else{
    passport.authenticate('bearer', function(err, user, info) {
      if ((err) || (!user)) {
        return res.sendStatus(401);
      }

      var scope = req.path;
      // sails.log('The accesstoken scopes = %s', info);
      log.debug('Current path = %s', req.path);
      // sails.log('Current scope = %s', scope);

      if (_.some(info, function(o) {
            return o == scope;
          })) {
        delete req.query.access_token;
        req.user = user;
        return next();
      } else {
        return res.sendStatus(401);
      };

    })(req, res);
  }
});

// app.use('/', index);
app.use('/users', users);
app.use('/qn', qn);
app.use('/type',type);
app.use('/picture',picture);
app.use('/admin',admin);
app.use('/recommend',recommend);
app.use('/article',article);
app.use('/console',console);
app.use('/project',project);
app.use('/systemlogs',systemlogs);
app.use('/brand',brand);
app.use('/experience',experience);
app.use('/openwx',openwx);
app.use('/basicinfo',basicinfo);
app.use('/feedback',feedback);
// app.post('/login',pass.postlogin);//登录认证

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
    log.error("Something went wrong:", err);
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
